#ifndef FILE_INFO_CODEC_H
#define FILE_INFO_CODEC_H 

#include <stdlib.h>
#include <stdio.h>
#include <muduo/base/Types.h>

const char spliterChar = '-';

namespace sdfs
{
namespace tracker
{

class FileInfoCodec
{
public:
	FileInfoCodec(const muduo::string& value):value_(value)
	{
		char *p = const_cast<char*>(value_.c_str());
		int j = 0;
		pointer[j++] = p;
		// char* pname = p;
		// char* pblockid;
		// char* plength;
		// char* poffset;
		while( *p != '\0')
		{
			while( *p != spliterChar && *p != '\0')
			{
				p++;
			}
			if( *p == '\0') break;
			pointer[j++] = p+1;
			*p = '\0';
			p++;
		}
		blockid_ = ::atoi(pointer[1]);
		offset_ = ::atol(pointer[2]);
		length_ = ::atol(pointer[3]);

	}
	~FileInfoCodec()
	{

	}

	muduo::string getStorageName()
	{
		return pointer[0];
	}

	int getBlockId()
	{
		return blockid_;
	}

	long getOffset()
	{
		return offset_;
	}

	long getLength()
	{
		return length_;
	}

	static muduo::string getValue(muduo::string& name, int blockid, long offset, long length)
	{
		char buf[64];
		snprintf(buf, 64, "%s%c%d%c%ld%c%ld", name.c_str(),spliterChar, blockid, spliterChar,
				offset, spliterChar, length);
		return buf;
	}


private:
	muduo::string value_;
	char* pointer[4];
	int blockid_;
	long offset_;
	long length_;
};



}
}

#endif